/*
 ***************************************************************************************
 *  Copyright (C) 2006 EsperTech, Inc. All rights reserved.                            *
 *  http://www.espertech.com/esper                                                     *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 ***************************************************************************************
 */
package com.espertech.esper.common.internal.event.bean.getter;

import com.espertech.esper.common.client.EventBean;
import com.espertech.esper.common.internal.support.SupportBean;
import com.espertech.esper.common.internal.supportunit.event.SupportEventBeanFactory;
import junit.framework.TestCase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

public class TestReflectionPropMethodGetter extends TestCase {
    EventBean unitTestBean;

    public void setUp() {
        SupportBean testEvent = new SupportBean();
        testEvent.setIntPrimitive(10);
        testEvent.setTheString("a");
        testEvent.setDoubleBoxed(null);

        unitTestBean = SupportEventBeanFactory.createObject(testEvent);
    }

    public void testGetter() throws Exception {
        ReflectionPropMethodGetter getter = makeGetter(SupportBean.class, "getIntPrimitive");
        assertEquals(10, getter.get(unitTestBean));

        getter = makeGetter(SupportBean.class, "getTheString");
        assertEquals("a", getter.get(unitTestBean));

        getter = makeGetter(SupportBean.class, "getDoubleBoxed");
        assertEquals(null, getter.get(unitTestBean));
    }

    public void testPerformance() throws Exception {
        ReflectionPropMethodGetter getter = makeGetter(SupportBean.class, "getIntPrimitive");

        log.info(".testPerformance Starting test");

        for (int i = 0; i < 10; i++)   // Change to 1E8 for performance testing
        {
            int value = (Integer) getter.get(unitTestBean);
            assertEquals(10, value);
        }

        log.info(".testPerformance Done test");
    }

    private ReflectionPropMethodGetter makeGetter(Class clazz, String methodName) throws Exception {
        Method method = clazz.getMethod(methodName, new Class[]{});

        ReflectionPropMethodGetter getter = new ReflectionPropMethodGetter(method, null, null);

        return getter;
    }

    private static final Logger log = LoggerFactory.getLogger(TestReflectionPropMethodGetter.class);
}
